﻿using Microsoft.Owin.Security.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Owin.Security.Custom.Providers
{
    public class BaseAccessTokenProvider : IAuthenticationTokenProvider
    {
        public Action<AuthenticationTokenCreateContext> OnCreate
        {
            get;
            set;
        }

        public Func<AuthenticationTokenCreateContext, Task> OnCreateAsync
        {
            get;
            set;
        }

        public Action<AuthenticationTokenReceiveContext> OnReceive
        {
            get;
            set;
        }

        public Func<AuthenticationTokenReceiveContext, Task> OnReceiveAsync
        {
            get;
            set;
        }

        public BaseAccessTokenProvider()
        {
            //OnReceive = (context) =>
            //{
            //    context.DeserializeTicket(context.Token);
            //};
        }

        public virtual void Create(AuthenticationTokenCreateContext context)
        {
            if (this.OnCreateAsync != null && this.OnCreate == null)
            {
                //throw new InvalidOperationException(Resources.Exception_AuthenticationTokenDoesNotProvideSyncMethods);
            }
            if (this.OnCreate != null)
            {
                this.OnCreate(context);
            }
        }

        public virtual async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            if (this.OnCreateAsync != null && this.OnCreate == null)
            {
                //throw new InvalidOperationException(Resources.Exception_AuthenticationTokenDoesNotProvideSyncMethods);
            }
            if (this.OnCreateAsync == null)
            {
                this.Create(context);
            }
            else
            {
                await this.OnCreateAsync(context);
            }
        }

        public virtual void Receive(AuthenticationTokenReceiveContext context)
        {
            if (this.OnReceiveAsync != null && this.OnReceive == null)
            {
                //throw new InvalidOperationException(Resources.Exception_AuthenticationTokenDoesNotProvideSyncMethods);
            }
            if (this.OnReceive != null)
            {
                this.OnReceive(context);
            }
        }

        public virtual async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
        {
            if (this.OnReceiveAsync != null && this.OnReceive == null)
            {
                //throw new InvalidOperationException(Resources.Exception_AuthenticationTokenDoesNotProvideSyncMethods);
            }
            if (this.OnReceiveAsync == null)
            {
                this.Receive(context);
            }
            else
            {
                await this.OnReceiveAsync(context);
            }
        }
    }
}
